﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Diagnostics;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;

using EasyStorage;

using YEngineLib;
using YEngineLib.Graphics;
using YEngineLib.Input;
using YEngineLib.State;
using YEngineLib.Util;

//using Microsoft.Xna.Framework;
//using Microsoft.Xna.Framework.Audio;
//using Microsoft.Xna.Framework.Content;
//using Microsoft.Xna.Framework.GamerServices;
//using Microsoft.Xna.Framework.Graphics;
//using Microsoft.Xna.Framework.Input;
//using Microsoft.Xna.Framework.Media;

using System.Threading;
using Spyfall.Objects;


namespace Spyfall.States
{
    class IntroState : YBaseGameState
    {
        IAsyncSaveDevice saveDevice;

        //Gamepad states used to determine buttons
        GamePadState currentGamePadState;
        GamePadState previousGameState;
        public PlayerIndex daPlaya;
        private Managers.ObjectManager objectManager;
        private Managers.MessageManager msgManager;
        Vector2 screenPosition; 
        SpriteFont spriteFont;
        private Viewport viewPort;
        List<String> menuOptions = new List<String>();
        //private IntroScreen background; 
        Texture2D IntroPic;
        Rectangle wholescreen;
        bool goToMainMenu;
        bool aWasPressed;
        PlayerSaveDevice sharedSaveDevice;

        public IntroState()
        {
            screenPosition = new Vector2(10.0f, 30.0f);
            objectManager = new Managers.ObjectManager();
            msgManager = new Managers.MessageManager();
            //background = new IntroScreen();
            //background.Initialize();
            wholescreen = new Rectangle(0, 0, 1280, 720);
            goToMainMenu = false;
            aWasPressed = false;


        }

        public override void LoadContent()
        {
            spriteFont = YEngine.Game.Content.Load<SpriteFont>("Fonts/SpriteFont1");
            IntroPic = YEngine.Game.Content.Load<Texture2D>("Graphics/splashart");
            LoadPercentage = 100.0f;
        }

        public override void OnEnter(string previousState)
        {
            currentGamePadState = GamePad.GetState(daPlaya);    //keeps it from flickering.
            viewPort = YEngine.Graphics.ScreenViewport;
            menuOptions.Add("TEST");
        }

        public override void OnExit(string nextState)
        {
            //get out of here!!!
        }

        public override void Input()
        {
            if (YEngine.Input.Keyboard.KeyPressed(Keys.Enter) )
            {
                YEngine.State.ChangeState("MenuState");
                return;
            }



            if (goToMainMenu)
            {
                YEngine.State.ChangeState("MenuState");
                return;
            }

            previousGameState = currentGamePadState;
            currentGamePadState = GamePad.GetState(daPlaya);
            //if (currentGamePadState.Buttons.A == ButtonState.Pressed && previousGameState.Buttons.A != ButtonState.Pressed)
            //{
            //    YEngine.State.ChangeState("MenuState");
            //    return;
            //}
            //if (currentGamePadState.Buttons.B == ButtonState.Pressed && previousGameState.Buttons.B != ButtonState.Pressed)
            //{
            //    YEngine.State.ChangeState("MenuState");
            //    return;
            //}
            if (currentGamePadState.Buttons.A == ButtonState.Pressed )
            {

                aWasPressed = true;

                for (int i = 0; i < 4; i++)
                {
                    if (GamePad.GetState((PlayerIndex)i).Buttons.A == ButtonState.Pressed)
                    {
                        daPlaya = (PlayerIndex)i;
                    }
                }


                // create and add our SaveDevice
                sharedSaveDevice = new PlayerSaveDevice(daPlaya);
                //ScreenManager.Game.Components.Add(sharedSaveDevice);

                // make sure we hold on to the device
                saveDevice = sharedSaveDevice;

            }

            if (aWasPressed)
            {

                // hook two event handlers to force the user to choose a new device if they cancel the
                // device selector or if they disconnect the storage device after selecting it
                sharedSaveDevice.DeviceSelectorCanceled +=
                    (s, e) => e.Response = SaveDeviceEventResponse.Force;
                sharedSaveDevice.DeviceDisconnected +=
                    (s, e) => e.Response = SaveDeviceEventResponse.Force;

                // prompt for a device on the first Update we can
                sharedSaveDevice.PromptForDevice();
                sharedSaveDevice.Update(YEngine.Time.GameTime);

                sharedSaveDevice.DeviceSelected += (s, e) =>
                {
                    //Save our save device to the global counterpart, so we can access it
                    //anywhere we want to save/load
                    Global.SaveDevice = (SaveDevice)s;

                    //Once they select a storage device, we can load the main menu.
                    //You'll notice I hard coded PlayerIndex.One here. You'll need to
                    //change that if you plan on releasing your game. I linked to an
                    //example on how to do that but here's the link if you need it.
                    //<a href="http://blog.nickgravelyn.com/2009/03/basic-handling-of-multiple-controllers/">http://blog.nickgravelyn.com/2009/03/basic-handling-of-multiple-controllers/</a>
                    //We need to perform a check to see if we're on the Press Start Screen.
                    //If a storage device is selected NOT from this page, we don't want to
                    //create a new Main Menu screen! (Thanks @FreelanceGames for the mention)
                    //if (this.IsActive)
                    //   ScreenManager.AddScreen(new MainMenuScreen(), PlayerIndex.One);
                    goToMainMenu = true;
                };

                //#if XBOX
                // add the GamerServicesComponent
                //           YEngine.Game.Components.Add(
                //               new Microsoft.Xna.Framework.GamerServices.GamerServicesComponent(ScreenManager.Game));
                //#endif

            }




            if (goToMainMenu)
            {
                YEngine.State.ChangeState("MenuState");
                return;
            }


            




        }

        public override void Update()
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                YEngine.Game.Exit();
            objectManager.UpdateObjects((float)YEngine.Time.GameTime.ElapsedGameTime.TotalSeconds);
        }

        public override void Draw()
        {
            YEngine.Graphics.SpriteBatch.Begin();
            //background.Draw();
            YEngine.Graphics.SpriteBatch.Draw(IntroPic, wholescreen, Color.White);
            YEngine.Graphics.SpriteBatch.DrawString(spriteFont, "IntroState Prototype", screenPosition, Color.White);

            YEngine.Graphics.SpriteBatch.End();
        }
    }
}
